
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{

    private Parser parser;
    private Room currentRoom;
    private Player player;
    
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }
    
    /**
     * Create all the rooms and link their exits together.
     */
   
  
    private void createRooms()
    {
        //Room outside, theater, pub, lab, office, classroom, parkingLot, toilet, fruitForest;
      
        // create the rooms
        /*outside = new Room("outside the main entrance of the university", new Thing("坚果", 10));
        theater = new Room("in a lecture theater", new Thing("苹果", 30));
        pub = new Room("in the campus pub");
        lab = new Room("in a computing lab");
        office = new Room("in the computing admin office", new Thing("火龙果", 50), new Monster("蝙蝠", 35));
        classroom = new Room("in the school", new Thing("香蕉", 25));
        parkingLot = new Room("in the parking lot", new Monster("老鼠", 15));
        toilet = new Room("in the toilet", new Monster("史莱姆", 10));
        fruitForest = new Room("in the furit forest!", new Thing("桃子", 20));*/
        Room blue = new Room("蓝色的房间\n只见面前是一片大海，有点不知所措，但旁边有一条小船。",new Thing("船",0),new Monster("鲨鱼", 15,30));
        Room qing = new Room("青色的房间\n这里是一片竹林，隐隐约约还听到咔嚓，咔嚓的声音...",new Thing("竹笋",50),new Monster("熊猫",30,30));
        Room gray = new Room("灰色的房间\n这里重重迷雾找不到方向，旁边有火把，我想向前一探究竟...",new Thing("火",0));
        Room glost= new Room("迷雾中\n周围全是迷雾，这时我想离开了。");
        Room milu = new Room("迷雾中\n你迷路了，再不回去你会死...");
        Room onelibrary = new Room("图书馆一楼\n这是一个巨大的图书馆，我在图书馆一楼，一阵阵敲钟声从头顶传来，响彻整个图书馆，让人感到十分压抑。");
        Room twolibrary = new Room("图书馆二楼\n这是一个巨大的图书馆，我在图书馆二楼，终于看到了钟响的源头，是两个铁球正在有规律的碰撞。",new Thing("铁球",0));
        Room gate = new Room("图书馆正门\n正门被锁上了这里应该出不去！\n");
        Room hall= new Room("森严的大厅\n这是初始位置大厅，这是一个圆形大厅，\n它的正中心有一条浮空金黄色的蛇雕像，它正咬着自己的尾巴。\n大厅的四周有四个门，分别是蓝色，灰色，青色，棕色。你不知道该如何是好，只能选择一个门去一探究竟。 ");
        Room home = new Room("家\n梦醒了———我看着墙上挂着曾经的照片，有些是曾经翘课去海边的，有些去竹林旅游的，还有很多很多。\n现在回想刚才发生的事，感觉是那样真实，看了看床头的闹钟,我静静的说道：'嗯该上学了'。\n完结散花！！",new Thing("番茄炒西红柿",10));
   
        
        //设置入口方向
    
        hall.setExit("east",blue);
        hall.setExit("south",qing);
        hall.setExit("west",gray);
        hall.setExit("north",onelibrary);
        hall.setExit("down",home);
        
        //blue
        blue.setExit("west",hall);
        
        //qing
        qing.setExit("north",hall);
        
        //gray
        gray.setExit("east",hall);
        gray.setExit("west",glost);
        
        //glost
        glost.setExit("west",milu);
        glost.setExit("east",milu);
        glost.setExit("north",milu);
        glost.setExit("south",milu);
        
        milu.setExit("east",glost);
        //onelibrary
        onelibrary.setExit("south",hall);
        
        onelibrary.setExit("up",twolibrary);
        
        //twolibrary
        twolibrary.setExit("down",onelibrary);

        currentRoom = hall;// start game outside
        
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("Thank you for playing.  Good bye.");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎游玩‘逃离hall’");
        System.out.println("你是一个闯入者，你忘记了你怎么来到这里");
        System.out.println("你想从这里出这里");
        System.out.println("输入 'help' 查看指令");
        System.out.println();
        System.out.println("你当前处于" + currentRoom.getDescription());
        currentRoom.printExits();
    }
 
    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        Thing thing = currentRoom.getThing();

        if(command.isUnknown()) {
            System.out.println("无效的指令");
            return false;
        }
 
        Word commandWord = command.getCommandWord();
        // if (commandWord.equals("help")) {
            // printHelp();
        // }
        // else if (commandWord.equals("go")) {
            // goRoom(command);
        // }
        // else if (commandWord.equals("look")) {
            // say(currentRoom);
        // }
        // else if (commandWord.equals("pick")) {
            // player.addToBag(thing);
            // currentRoom.remove();
        // }
        // else if (commandWord.equals("check")) {
            // player.checkAndPrint();
        // }
        // else if (commandWord.equals("eat")) {
            // player.eatThing(command);
        // }
        // else if (commandWord.equals("quit")) {
            // wantToQuit = quit(command);
        // }
        switch(commandWord) {
            case HELP: 
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case LOOK:
                say(currentRoom);
                break;
            case PICK:
                player.addToBag(thing);
                currentRoom.remove();
                break;
            case CHECK:
                player.checkAndPrint();
                break;
            case EAT:
                player.eatThing(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
        }
        return wantToQuit;
    }
    
    private void say(Room currentRoom) {
        System.out.print("这里");
        if (currentRoom.getThing() == null) {
            System.out.println("没有你想捡的物品。");
        }
        else System.out.println("有"+currentRoom.getThing().getName());
        if (currentRoom.getMonster() == null) {
            System.out.println("目前看来这里安全!");
        }
        else {
            player.hurt(currentRoom.getMonster().getDamage());
            System.out.println("我的天哪! 那里有一只" + currentRoom.getMonster().getMonsterName() +"，它发现了你"+ " !");
            System.out.println("你被咬伤了！");
            System.out.println("你损失了" + currentRoom.getMonster().getDamage() + " 点体力!");
            currentRoom.removeMonster();
        }
    }
    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("You are lost,you want to leave.");
        System.out.println("around at the hall.");
        System.out.println();
        System.out.println("Your command words are:");
        System.out.println("   go --前往下一个房间(要在后面追加方向)");
        System.out.println("   help --获取游戏帮助");
        System.out.println("   look -- 检查房间内安全状况");
        System.out.println("   pick -- 拾起东西");
        System.out.println("   check -- 查看背包中的物品");
        System.out.println("   eat -- 吃背包中的补给品(在后面追加物品)");
        System.out.println("   quit -- 退出游戏");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Go where?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);
        
        if (nextRoom == null) {
            System.out.println("此路不通");
        }
        else {
            player.step();
            currentRoom = nextRoom;
            System.out.println("你当前处于" + currentRoom.getDescription());
            currentRoom.printExits();
        }
    }

    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}
